Connectivity Software User's Guide and Reference
Publishing Commands in Sparkplug Consumer
Rapid Toolkit for Sparkplug > Concepts > Developing Sparkplug Host Applications > Publishing Commands in Sparkplug Consumer
In This Topic

Introduction

Besides the common flow of data from Sparkplug edge nodes (and their devices) to the MQTT broker and from there to Sparkplug host applications (consumers), Sparkplug also supports a data flow in the reverse direction: The host applications (consumers) can send data through the MQTT broker to the edge nodes (and their devices). Such Sparkplug commands contain new data for the metrics of the target edge node or device.

Rapid Toolkit for Sparkplug has two groups of methods for publishing Sparkplug commands:

The methods for publishing Sparkplug commands are synchronous, and throw an exception in case of an error. For more information, see Rapid Toolkit for Sparkplug Consumer Error Model.

Publishing Single Command Metric

In order to publish a Sparkplug command containing new data for a single metric, call one of the following methods:

The following example illustrates publishing a single metric to an edge node.

.NET

// This example shows how to publish a command with single metric for a given edge node.
//
// In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
// Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using OpcLabs.EasySparkplug;
using OpcLabs.EasySparkplug.OperationModel;

namespace SparkplugDocExamples.Consumer._EasySparkplugConsumer
{
    partial class PublishEdgeNodeMetric
    {
        public static void Overload1()
        {
            // Note that the default port for the "mqtt" scheme is 1883.
            var hostDescriptor = new SparkplugHostDescriptor("mqtt://localhost");

            // Instantiate the consumer object.
            var consumer = new EasySparkplugConsumer();

            Console.WriteLine("Publishing...");
            try
            {
                consumer.PublishEdgeNodeMetric(hostDescriptor, 
                    "easyGroup", "easySparkplugDemo", "Simple", 
                    new SparkplugMetricData(42));   // the command metric value
            }
            catch (SparkplugException sparkplugException)
            {
                Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}");
                return;
            }

            Console.WriteLine("Finished.");
        }
    }
}
' This example shows how to publish a command with single metric for a given edge node.
'
' In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
' Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports OpcLabs.EasySparkplug
Imports OpcLabs.EasySparkplug.OperationModel

Namespace Global.SparkplugDocExamples.Consumer._EasySparkplugConsumer
    Partial Class PublishEdgeNodeMetric
        Public Shared Sub Overload1()
            ' Note that the default port for the "mqtt" scheme is 1883.
            Dim hostDescriptor = New SparkplugHostDescriptor("mqtt://localhost")

            ' Instantiate the consumer object.
            Dim consumer = New EasySparkplugConsumer()

            Console.WriteLine("Publishing...")
            Try
                consumer.PublishEdgeNodeMetric(hostDescriptor,
                    "easyGroup", "easySparkplugDemo", "Simple",
                    New SparkplugMetricData(42)) ' the command metric value
            Catch sparkplugException As SparkplugException
                Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}")
                Return
            End Try

            Console.WriteLine("Finished.")
        End Sub
    End Class
End Namespace

 

A similar example, but with publishing a value that changes every 2 seconds, is below (this example is useful as a "playground", because the stream of increasing values can be easier observed on the edge node).

Example

.NET

// This example shows how to send an ever-incrementing value to a Sparkplug metric.
//
// In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
// Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using OpcLabs.EasySparkplug;
using OpcLabs.EasySparkplug.OperationModel;
using System;
using System.Threading;

namespace SparkplugDocExamples.Consumer._EasySparkplugConsumer
{
    partial class PublishEdgeNodeMetric
    {
        public static void Incrementing()
        {
            // Note that the default port for the "mqtt" scheme is 1883.
            var hostDescriptor = new SparkplugHostDescriptor("mqtt://localhost");

            // Instantiate the consumer object.
            var consumer = new EasySparkplugConsumer();

            //
            Console.WriteLine("Publishing... (press any key to stop)");
            int i = 0;

            do
            {
                Console.WriteLine($"@{DateTime.Now}: Publishing {i}");
                try
                {
                    consumer.PublishEdgeNodeMetric(hostDescriptor,
                        "easyGroup", "easySparkplugDemo", "Simple",
                        new SparkplugMetricData(i));   // the command metric value
                }
                catch (SparkplugException sparkplugException)
                {
                    Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}");
                    return;
                }
                i = unchecked((i + 1) & 0x7FFFFFFF);
                Thread.Sleep(2 * 1000);
            } while (!Console.KeyAvailable);

            Console.WriteLine("Finished.");
        }
    }
}
' This example shows how to send an ever-incrementing value to a Sparkplug metric.
'
' In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
' Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports System.Threading
Imports OpcLabs.EasySparkplug
Imports OpcLabs.EasySparkplug.OperationModel

Namespace Global.SparkplugDocExamples.Consumer._EasySparkplugConsumer
    Partial Class PublishEdgeNodeMetric
        Public Shared Sub Incrementing()
            ' Note that the default port for the "mqtt" scheme is 1883.
            Dim hostDescriptor = New SparkplugHostDescriptor("mqtt://localhost")

            ' Instantiate the consumer object.
            Dim consumer = New EasySparkplugConsumer()

            ' 
            Console.WriteLine("Publishing... (press any key to stop)")
            Dim i = 0

            Do
                Console.WriteLine($"@{DateTime.Now}: Publishing {i}")
                Try
                    consumer.PublishEdgeNodeMetric(hostDescriptor,
                        "easyGroup", "easySparkplugDemo", "Simple",
                        New SparkplugMetricData(i)) ' the command metric value
                Catch sparkplugException As SparkplugException
                    Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}")
                    Return
                End Try
                i = CInt((CLng(i) + 1) And &H7FFFFFFF)
                Thread.Sleep(2 * 1000)
            Loop Until Console.KeyAvailable

            Console.WriteLine("Finished.")
        End Sub
    End Class
End Namespace

 

The following example illustrates publishing a single metric to a device.

.NET

// This example shows how to publish a command with single metric for a given device.
//
// In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
// Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using OpcLabs.EasySparkplug;
using OpcLabs.EasySparkplug.OperationModel;

namespace SparkplugDocExamples.Consumer._EasySparkplugConsumer
{
    partial class PublishDeviceMetric
    {
        public static void Overload1()
        {
            // Note that the default port for the "mqtt" scheme is 1883.
            var hostDescriptor = new SparkplugHostDescriptor("mqtt://localhost");

            // Instantiate the consumer object.
            var consumer = new EasySparkplugConsumer();

            Console.WriteLine("Publishing...");
            try
            {
                consumer.PublishDeviceMetric(hostDescriptor, 
                    "easyGroup", "easySparkplugDemo", "demo", "Simple", 
                    new SparkplugMetricData(42));   // the command metric value
            }
            catch (SparkplugException sparkplugException)
            {
                Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}");
                return;
            }

            Console.WriteLine("Finished.");
        }
    }
}
' This example shows how to publish a command with single metric for a given device.
'
' In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
' Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports OpcLabs.EasySparkplug
Imports OpcLabs.EasySparkplug.OperationModel

Namespace Global.SparkplugDocExamples.Consumer._EasySparkplugConsumer
    Partial Class PublishDeviceMetric
        Public Shared Sub Overload1()
            ' Note that the default port for the "mqtt" scheme is 1883.
            Dim hostDescriptor = New SparkplugHostDescriptor("mqtt://localhost")

            ' Instantiate the consumer object.
            Dim consumer = New EasySparkplugConsumer()

            Console.WriteLine("Publishing...")
            Try
                consumer.PublishDeviceMetric(hostDescriptor,
                    "easyGroup", "easySparkplugDemo", "demo", "Simple",
                    New SparkplugMetricData(42)) ' the command metric value
            Catch sparkplugException As SparkplugException
                Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}")
                Return
            End Try

            Console.WriteLine("Finished.")
        End Sub
    End Class
End Namespace

 

Publishing Multiple Command Metrics

In order to publish a Sparkplug command containing new data for multiple metrics, call one of the following methods:

The following example illustrates publishing multiple metrics to an edge node.

 

.NET

// This example shows how to publish a payload with multiple command metrics for a given edge node.
//
// In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
// Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using OpcLabs.EasySparkplug;
using OpcLabs.EasySparkplug.OperationModel;

namespace SparkplugDocExamples.Consumer._EasySparkplugConsumer
{
    class PublishEdgeNodePayload
    {
        public static void Overload1()
        {
            // Note that the default port for the "mqtt" scheme is 1883.
            var hostDescriptor = new SparkplugHostDescriptor("mqtt://localhost");

            // Instantiate the consumer object.
            var consumer = new EasySparkplugConsumer();

            // Create the payload with multiple command metrics.
            var payload = new SparkplugPayload
            {
                { "Simple", new SparkplugMetricData(42) },
                { "Simple2", new SparkplugMetricData(43) }
            };
            
            Console.WriteLine("Publishing...");
            try
            {
                consumer.PublishEdgeNodePayload(hostDescriptor, "easyGroup", "easySparkplugDemo", payload);
            }
            catch (SparkplugException sparkplugException)
            {
                Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}");
                return;
            }

            Console.WriteLine("Finished.");
        }
    }
}
' This example shows how to publish a payload with multiple command metrics for a given edge node.
'
' In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
' Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports OpcLabs.EasySparkplug
Imports OpcLabs.EasySparkplug.OperationModel

Namespace Global.SparkplugDocExamples.Consumer._EasySparkplugConsumer
    Class PublishEdgeNodePayload
        Public Shared Sub Overload1()
            ' Note that the default port for the "mqtt" scheme is 1883.
            Dim hostDescriptor = New SparkplugHostDescriptor("mqtt://localhost")

            ' Instantiate the consumer object.
            Dim consumer = New EasySparkplugConsumer()

            ' Create the payload with multiple command metrics.
            Dim payload = New SparkplugPayload From
            {
                {"Simple", New SparkplugMetricData(42)},
                {"Simple2", New SparkplugMetricData(43)}
            }

            Console.WriteLine("Publishing...")
            Try
                consumer.PublishEdgeNodePayload(hostDescriptor, "easyGroup", "easySparkplugDemo", payload)
            Catch sparkplugException As SparkplugException
                Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}")
                Return
            End Try

            Console.WriteLine("Finished.")
        End Sub
    End Class
End Namespace

 

Including a Timestamp

In many cases, Sparkplug commands are send without a timestamp (they contain just the new value of the metric). Missing timestamp is indicated by the default value (DateTime.MinValue) in the Timestamp Property of the SparkplugMetricData object that you pass in the command. If you need to include am explicit timestamp in the Sparkplug command, create the SparkplugMetricData object so that it contains the desired timestamp.

The following example illustrates how a chosen timestamp can be published with the Sparkplug command.

Example

.NET

// This example shows how to publish a command with single metric for a given edge node, including timestamp.
//
// In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
// Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using OpcLabs.EasySparkplug;
using OpcLabs.EasySparkplug.OperationModel;

namespace SparkplugDocExamples.Consumer._EasySparkplugConsumer
{
    partial class PublishEdgeNodeMetric
    {
        public static void Timestamp()
        {
            // Note that the default port for the "mqtt" scheme is 1883.
            var hostDescriptor = new SparkplugHostDescriptor("mqtt://localhost");

            // Instantiate the consumer object.
            var consumer = new EasySparkplugConsumer();

            Console.WriteLine("Publishing...");
            try
            {
                // Create the command metric data: value and timestamp.
                var metricData = new SparkplugMetricData(42, DateTime.UtcNow.AddMinutes(-10));
                
                consumer.PublishEdgeNodeMetric(hostDescriptor, 
                    "easyGroup", "easySparkplugDemo", "Simple",
                    metricData);
            }
            catch (SparkplugException sparkplugException)
            {
                Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}");
                return;
            }

            Console.WriteLine("Finished.");
        }
    }
}
' This example shows how to publish a command with single metric for a given edge node, including timestamp.
'
' In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
' Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports OpcLabs.EasySparkplug
Imports OpcLabs.EasySparkplug.OperationModel

Namespace Global.SparkplugDocExamples.Consumer._EasySparkplugConsumer
    Partial Class PublishEdgeNodeMetric
        Public Shared Sub Timestamp()
            ' Note that the default port for the "mqtt" scheme is 1883.
            Dim hostDescriptor = New SparkplugHostDescriptor("mqtt://localhost")

            ' Instantiate the consumer object.
            Dim consumer = New EasySparkplugConsumer()

            Console.WriteLine("Publishing...")
            Try
                ' Create the command metric data: value and timestamp.
                Dim metricData = New SparkplugMetricData(42, DateTime.UtcNow.AddMinutes(-10))

                consumer.PublishEdgeNodeMetric(hostDescriptor,
                    "easyGroup", "easySparkplugDemo", "Simple",
                    metricData)
            Catch sparkplugException As SparkplugException
                Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}")
                Return
            End Try

            Console.WriteLine("Finished.")
        End Sub
    End Class
End Namespace

 

Working with Data Types

Rapid Toolkit for Sparkplug provides automatic conversion from .NET types to Sparkplug types. The correspondences between the two type systems are described in the article Data Types in Rapid Toolkit for Sparkplug. Therefore, it most cases, publishing the data "just works".

For example, in the following code, Rapid Toolkit for Sparkplug publishes the data with Sparkplug data type Int32Array, because the metric value passed in is a .NET array of Int32 values.

Example

.NET

// This example shows how to publish a device command to a metric that is an array of Int32.
//
// In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
// Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using OpcLabs.EasySparkplug;
using OpcLabs.EasySparkplug.OperationModel;

namespace SparkplugDocExamples.Consumer._EasySparkplugConsumer
{
    partial class PublishDeviceMetric
    {
        public static void Int32Array()
        {
            // Note that the default port for the "mqtt" scheme is 1883.
            var hostDescriptor = new SparkplugHostDescriptor("mqtt://localhost");

            // Instantiate the consumer object.
            var consumer = new EasySparkplugConsumer();

            Console.WriteLine("Publishing...");
            try
            {
                var arrayValue = new Int32[] { 11111, 22222, 33333, 44444, 55555, 66666, 77777 };
                consumer.PublishDeviceMetric(hostDescriptor, 
                    "easyGroup", "easySparkplugDemo", "data", "Static/Int32ArrayValue", 
                    new SparkplugMetricData(arrayValue));   // the command metric value
            }
            catch (SparkplugException sparkplugException)
            {
                Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}");
                return;
            }

            Console.WriteLine("Finished.");
        }
    }
}
' This example shows how to publish a device command to a metric that is an array of Int32.
'
' In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
' Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports OpcLabs.EasySparkplug
Imports OpcLabs.EasySparkplug.OperationModel

Namespace Global.SparkplugDocExamples.Consumer._EasySparkplugConsumer
    Partial Class PublishDeviceMetric
        Public Shared Sub Int32Array()
            ' Note that the default port for the "mqtt" scheme is 1883.
            Dim hostDescriptor = New SparkplugHostDescriptor("mqtt://localhost")

            ' Instantiate the consumer object.
            Dim consumer = New EasySparkplugConsumer()

            Console.WriteLine("Publishing...")
            Try
                Dim arrayValue = New Int32() {11111, 22222, 33333, 44444, 55555, 66666, 77777}
                consumer.PublishDeviceMetric(hostDescriptor,
                    "easyGroup", "easySparkplugDemo", "data", "Static/Int32ArrayValue",
                    New SparkplugMetricData(arrayValue)) ' the command metric value
            Catch sparkplugException As SparkplugException
                Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}")
                Return
            End Try

            Console.WriteLine("Finished.")
        End Sub
    End Class
End Namespace

 

There are cases in which Rapid Toolkit for Sparkplug cannot always determine the desired data type. This is because multiple Sparkplug data types can map to a single .NET type, which you figure out easily by examining the table incldued in the article Data Types in Rapid Toolkit for Sparkplug.

Sparkplug types String and Text are both represented by .NET String type. When Rapid Toolkit for Sparkplug encounters .NET String as a metric value, it chooses Sparkplug String type for it. If your intent was to publish Sparkplug Text type instead, you need to specify the Sparkplug data type explicitly. The following example illustrates how to do it.

Examples

.NET

// This example shows how to publish a command with single metric for a given device, specifying the data type.
//
// In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
// Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using OpcLabs.EasySparkplug;
using OpcLabs.EasySparkplug.OperationModel;

namespace SparkplugDocExamples.Consumer._EasySparkplugConsumer
{
    partial class PublishDeviceMetric
    {
        public static void DataType()
        {
            // Note that the default port for the "mqtt" scheme is 1883.
            var hostDescriptor = new SparkplugHostDescriptor("mqtt://localhost");

            // Instantiate the consumer object.
            var consumer = new EasySparkplugConsumer();

            Console.WriteLine("Publishing...");
            try
            {
                // Create the command metric data, specifying the value and data type.
                // Note that without the explicitly specified data type, SparkplugDataType.String would be used here.
                var metricData = new SparkplugMetricData("abc", SparkplugDataType.Text);
                
                consumer.PublishDeviceMetric(hostDescriptor,
                    "easyGroup", "easySparkplugDemo", "data", "Static/TextValue",
                    metricData);   
            }
            catch (SparkplugException sparkplugException)
            {
                Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}");
                return;
            }

            Console.WriteLine("Finished.");
        }
    }
}
' This example shows how to publish a command with single metric for a given device, specifying the data type.
'
' In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
' Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports OpcLabs.EasySparkplug
Imports OpcLabs.EasySparkplug.OperationModel

Namespace Global.SparkplugDocExamples.Consumer._EasySparkplugConsumer
    Partial Class PublishDeviceMetric
        Public Shared Sub DataType()
            ' Note that the default port for the "mqtt" scheme is 1883.
            Dim hostDescriptor = New SparkplugHostDescriptor("mqtt://localhost")

            ' Instantiate the consumer object.
            Dim consumer = New EasySparkplugConsumer()

            Console.WriteLine("Publishing...")
            Try
                ' Create the command metric data, specifying the value and data type.
                ' Note that without the explicitly specified data type, SparkplugDataType.String would be used here.
                Dim metricData = New SparkplugMetricData("abc", SparkplugDataType.Text)

                consumer.PublishDeviceMetric(hostDescriptor,
                    "easyGroup", "easySparkplugDemo", "data", "Static/TextValue",
                    metricData)
            Catch sparkplugException As SparkplugException
                Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}")
                Return
            End Try

            Console.WriteLine("Finished.")
        End Sub
    End Class
End Namespace

 

Sparkplug types Bytes and UInt8Array are both represented by .NET array of Byte. When Rapid Toolkit for Sparkplug encounters .NET Byte[] as a metric value, it chooses Sparkplug Bytes type for it, as illustrated in the following example. If you wanted to publish Sparkplug UInt8Array again, you would need to specify the Sparkplug data explicitly, as described above.

Example

.NET

// This example shows how to publish a device command to a metric that is of type Bytes.
//
// In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
// Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using OpcLabs.EasySparkplug;
using OpcLabs.EasySparkplug.OperationModel;

namespace SparkplugDocExamples.Consumer._EasySparkplugConsumer
{
    partial class PublishDeviceMetric
    {
        public static void Bytes()
        {
            // Note that the default port for the "mqtt" scheme is 1883.
            var hostDescriptor = new SparkplugHostDescriptor("mqtt://localhost");

            // Instantiate the consumer object.
            var consumer = new EasySparkplugConsumer();

            Console.WriteLine("Publishing...");
            try
            {
                consumer.PublishDeviceMetric(hostDescriptor, 
                    "easyGroup", "easySparkplugDemo", "data", "Static/BytesValue", 
                    new SparkplugMetricData(new byte[] { 11, 22, 33, 44, 55 }));   // the command metric value
            }
            catch (SparkplugException sparkplugException)
            {
                Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}");
                return;
            }

            Console.WriteLine("Finished.");
        }
    }
}
' This example shows how to publish a device command to a metric that is of type Bytes.
'
' In order to publish or observe messages for this example, start the SparkplugEdgeNodeConsoleDemo program first.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
' Sparkplug examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports OpcLabs.EasySparkplug
Imports OpcLabs.EasySparkplug.OperationModel

Namespace Global.SparkplugDocExamples.Consumer._EasySparkplugConsumer
    Partial Class PublishDeviceMetric
        Public Shared Sub Bytes()
            ' Note that the default port for the "mqtt" scheme is 1883.
            Dim hostDescriptor = New SparkplugHostDescriptor("mqtt://localhost")

            ' Instantiate the consumer object.
            Dim consumer = New EasySparkplugConsumer()

            Console.WriteLine("Publishing...")
            Try
                consumer.PublishDeviceMetric(hostDescriptor,
                    "easyGroup", "easySparkplugDemo", "data", "Static/BytesValue",
                    New SparkplugMetricData(New Byte() {11, 22, 33, 44, 55})) ' the command metric value
            Catch sparkplugException As SparkplugException
                Console.WriteLine($"*** Failure: {sparkplugException.GetBaseException().Message}")
                Return
            End Try

            Console.WriteLine("Finished.")
        End Sub
    End Class
End Namespace

 

 

Sparkplug is a trademark of Eclipse Foundation, Inc. "MQTT" is a trademark of the OASIS Open standards consortium. Other related terms are trademarks of their respective owners. Any use of these terms on this site is for descriptive purposes only and does not imply any sponsorship, endorsement or affiliation.